//
//  KKAnimationKit.m
//  GCDDemo
//
//  Created by 程强 on 16/7/26.
//  Copyright © 2016年 程强. All rights reserved.
//

#import "KKAnimationKit.h"

@implementation KKAnimationKit
+ (CABasicAnimation *)animationWithKeyPath:(NSString *)keyPath fromValue:(CGFloat)from toValue:(CGFloat)to
{
    CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:keyPath];
    animation.fromValue = [NSNumber numberWithFloat:from];
    animation.toValue = [NSNumber numberWithFloat:to];
    animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut];
    return animation;
}
+ (void)animationTimeInterval:(NSTimeInterval)time scaleFrom:(CGFloat)fromValue to:(CGFloat)toValue inView:(UIView *)view autoreverse:(BOOL)reverse
{
    CABasicAnimation *animation = [KKAnimationKit animationWithKeyPath:@"transform.scale" fromValue:fromValue toValue:toValue];
    animation.duration = time;
    animation.repeatCount = 1;
    animation.autoreverses = reverse;
    animation.removedOnCompletion = NO;
    animation.fillMode = kCAFillModeForwards;
    [view.layer addAnimation:animation forKey:@"transform.scale"];
}
+ (void)animationTimeInterval:(NSTimeInterval)time radiusFrom:(CGFloat)fromValue to:(CGFloat)toValue inView:(UIView *)view
{
    CABasicAnimation *animation = [KKAnimationKit animationWithKeyPath:@"cornerRadius" fromValue:fromValue toValue:toValue];
    animation.duration = time;
    animation.repeatCount = CGFLOAT_MAX;
    animation.autoreverses = YES;
    [view.layer addAnimation:animation forKey:@"cornerRadius"];
}
+ (void)animationTimeInterval:(NSTimeInterval)time inView:(UIView *)view
{
    UIBezierPath *path = [UIBezierPath bezierPathWithArcCenter:CGPointMake(100 / 2.f, 100 / 2.f)
                                                        radius:20
                                                    startAngle:M_PI/2*-1
                                                      endAngle:M_PI * 3/2
                                                     clockwise:YES];

    CAShapeLayer *layer = [CAShapeLayer layer];
    layer.frame         = view.bounds;                // 与showView的frame一致
    layer.strokeColor   = [UIColor greenColor].CGColor;   // 边缘线的颜色
    layer.fillColor     = [UIColor clearColor].CGColor;   // 闭环填充的颜色
    layer.lineCap       = kCALineCapButt;               // 边缘线的类型
    layer.path          = path.CGPath;                    // 从贝塞尔曲线获取到形状
    layer.lineWidth     = 30.0f;                           // 线条宽度
    layer.strokeStart   = 0.0f;
    layer.strokeEnd     = 0.0f;

    // 将layer添加进图层
    [view.layer addSublayer:layer];

    CABasicAnimation *pulse = [CABasicAnimation animationWithKeyPath:@"strokeEnd"];
    pulse.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut];
    pulse.duration = time ;
    pulse.repeatCount = 1;
    pulse.fromValue = [NSNumber numberWithFloat:0];
    pulse.toValue = [NSNumber numberWithFloat:1.0];
    layer.strokeEnd     = 1.0f;
    [layer addAnimation:pulse forKey:@"strokeEnd"];
}
+ (void)transformTimeInterval:(NSTimeInterval)time rotation:(CGFloat)rote inView:(UIView *)view
{
    CGAffineTransform transform= CGAffineTransformMakeRotation(M_PI*rote);
    /*关于M_PI
     #define M_PI     3.14159265358979323846264338327950288
     其实它就是圆周率的值，在这里代表弧度，相当于角度制 0-360 度，M_PI=180度
     旋转方向为：顺时针旋转
     */
    
    [UIView animateWithDuration:time animations:^{
        view.transform = transform;//旋转
    } completion:^(BOOL finished) {
        
    }];
}
+ (void)cartAnimationTimeInterval:(NSTimeInterval)time inView:(UIView *)view toFrame:(CGRect)frame complete:(void(^)())block
{
    CABasicAnimation* rotationAnimation;
    rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
    rotationAnimation.toValue = [NSNumber numberWithFloat: M_PI * 11.0 ];
    rotationAnimation.duration = 1.0;
    rotationAnimation.cumulative = YES;
    rotationAnimation.repeatCount = 0;
    
    //这个是让旋转动画慢于缩放动画执行
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [view.layer addAnimation:rotationAnimation forKey:@"rotationAnimation"];
    });
    
    [UIView animateWithDuration:time animations:^{
        view.frame=frame;
    } completion:^(BOOL finished) {
        //动画完成后做的事
        if (block) {
            block();
        }
    }];
}
@end
